package com.platform.common.utils;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

//@Slf4j
//@Component
public class ImUtils {

    private final static MediaType CONTENT_TYPE = MediaType.parse("application/json;charset=utf-8");
    private final static String TOKEN_URL = "http://a1.easemob.com/1106200323168889/gongchengfuwuyun/token";
    private final static String USERS_URL = "http://a1.easemob.com/1106200323168889/gongchengfuwuyun/users";

//    @Autowired
//    private JedisUtil jedisUtil;

//    private  final Map map = MapUtil.of(
//            "grant_type", "client_credentials",
//            "client_id","YXA6-YthkmrPTW24ZAQBzPQmxQ",
//            "client_secret","YXA6xh8qycqcBX9rRoe-N-xra3ZBVlI"
//    );
//
//
//    public String getToken() throws IOException {
//
//        String access_token = jedisUtil.get("im_access_token");
//        if(StringUtils.isNotBlank(access_token)){
//            return access_token;
//        }
//
//        String json = JSON.toJSONString(map);
//        RequestBody body = RequestBody.create(CONTENT_TYPE, json);
//        Request request = new Request.Builder().post(body).url(TOKEN_URL).build();
//        Call call = new OkHttpClient().newCall(request);
//        Response response  = call.execute();
//
//        TokenInfo token = null;
//        if(response.isSuccessful()) {
//            token = JSON.parseObject(response.body().string(), TokenInfo.class);
//            jedisUtil.set("im_access_token", token.getAccess_token(),token.getExpires_in().intValue() * 60 );
//        }
//
//        return token.getAccess_token();
//    }


    public static boolean users(String username,String password,String nickname) {
        Map map = MapUtil.of(
                "username", username,
                "password",password,
                "nickname",nickname
        );
        String json = JSON.toJSONString(map);
        RequestBody body = RequestBody.create(CONTENT_TYPE, json);
        Request request = new Request.Builder().post(body).url(USERS_URL).build();
        Call call = new OkHttpClient().newCall(request);
        Response response  = null;
        try {
            response = call.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response.isSuccessful();
    }

//    public static void main(String[] args) throws IOException {
//        ImUtils.users("ganyimeng1","123456","甘一蒙");
//    }



}
//@Data
//class TokenInfo {
//    String application;
//    String access_token;
//    Long expires_in;
//}